package com.tonicgb.shared.config;

import com.tonicgb.shared.controller.MyCustomErrorController;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author zhaiyujin
 * @date 2021-09-01 14:09:30
 * @说明 覆盖异常配置
 */
@Configuration
@AutoConfigureBefore(ErrorMvcAutoConfiguration.class)
public class ExceptionConfig implements WebMvcConfigurer {
    //用来获取配置信息
    private final ServerProperties serverProperties;
    //用来获取自定义错误
    private final ErrorAttributes errorAttributes;
     ExceptionConfig(ServerProperties serverProperties,ErrorAttributes errorAttributes) {
        this.serverProperties = serverProperties;
        this.errorAttributes = errorAttributes;
    }
    @Bean
    @ConditionalOnMissingBean(
            value = {ErrorController.class},
            search = SearchStrategy.CURRENT
    )
    public MyCustomErrorController basicResponseErrorController() {

        return new MyCustomErrorController(errorAttributes, serverProperties.getError());
    }




}
